package com.gzy.cs5.controller;

import com.gzy.cs5.Repository.InventoryRepository;
import com.gzy.cs5.Repository.MemoryInventoryRepository;
import com.gzy.cs5.Repository.MemoryOrderRepository;
import com.gzy.cs5.Repository.OrderRepository;
import com.gzy.cs5.exception.BusinessException;
import com.gzy.cs5.exception.ErrorCode;
import com.gzy.cs5.lang.OrderStatus;
import com.gzy.cs5.model.Item;
import com.gzy.cs5.model.InventoryItem;
import com.gzy.cs5.model.Order;
import com.gzy.cs5.service.InventoryService;
import com.gzy.cs5.service.OrderService;
import com.gzy.cs5.service.SimpleInventoryService;
import com.gzy.cs5.service.SimpleOrderService;

import java.time.LocalDateTime;
import java.util.List;

public class OrderController {
    private OrderService orderService;
    private InventoryService inventoryService; // 添加库存服务

    public OrderController() {
        OrderRepository orderRepository = new MemoryOrderRepository();
        this.orderService = new SimpleOrderService(orderRepository);
        InventoryRepository inventoryRepository = new MemoryInventoryRepository();
        this.inventoryService = new SimpleInventoryService(inventoryRepository); // 初始化库存服务
    }

    public void createOrder(List<Item> items, boolean isVIP) {
        try {
            // 检查库存
            for (Item item : items) {
                InventoryItem inventoryItem = inventoryService.getItemById(item.getItemId());
                if (inventoryItem == null || inventoryItem.getQuantity() < item.getStock()) { // 修改: 使用 item.getStock() 替换为正确的数量
                    throw new BusinessException(ErrorCode.ITEM_OUT_OF_STOCK, "库存不足: " + item.getName());
                }
            }

            Order order = orderService.createOrder(items, isVIP);
            System.out.println("订单创建成功: " + order);
        } catch (BusinessException e) {
            System.out.println("订单创建失败: " + e.getMessage());
        }
    }

    public void cancelOrder(String orderId) {
        if (orderId == null) {
            System.out.println("订单ID未找到，无法取消订单");
            return;
        }

        try {
            orderService.cancelOrder(orderId, LocalDateTime.now());
            System.out.println("订单取消成功: " + orderId);
        } catch (BusinessException e) {
            System.out.println("订单取消失败: " + e.getMessage());
        }
    }

    public void getAllOrders() {
        try {
            List<Order> orders = orderService.getAllOrders();
            System.out.println("所有订单: " + orders);
        } catch (Exception e) {
            System.out.println("获取订单失败: " + e.getMessage());
        }
    }

    public void getOrderById(String orderId) {
        if (orderId == null) {
            System.out.println("订单ID未找到，无法获取订单详情");
            return;
        }

        try {
            Order order = orderService.getOrderById(orderId);
            if (order != null) {
                System.out.println("订单详情: " + order);
            } else {
                System.out.println("订单不存在: " + orderId);
            }
        } catch (Exception e) {
            System.out.println("获取订单详情失败: " + e.getMessage());
        }
    }

    public void updateOrderStatus(String orderId, OrderStatus status) {
        if (orderId == null) {
            System.out.println("订单ID未找到，无法更新订单状态");
            return;
        }

        try {
            orderService.updateOrderStatus(orderId, status);
            System.out.println("订单状态更新成功: " + orderId);
        } catch (BusinessException e) {
            System.out.println("订单状态更新失败: " + e.getMessage());
        }
    }

    public void processOrder() {
        System.out.println("处理订单...");
        try {
            List<Order> orders = orderService.getAllOrders();
            for (Order order : orders) {
                System.out.println("订单详情: " + order);
            }
        } catch (Exception e) {
            System.out.println("处理订单失败: " + e.getMessage());
        }
    }
}
